<template>
	  <div class="main">
      <h1 class="title">Hello World</h1>
	    <el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2" class="demo-ruleForm">
      
      <el-form-item prop="userName">
        <el-input v-model="ruleForm2.userName">
           <template slot="prepend">帐号:</template>
        </el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input type="password" v-model="ruleForm2.password">
          <template slot="prepend">密码:</template>
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-button class="button" type="primary" @click="handle">登入</el-button>
      </el-form-item>

      </el-form>
	  
	  </div>

    
</template>

<script>
export default {
  data () {
    let validateUser = (rule, value, callback) => {
      if(!value){
        return callback(new Error('账户不能为空'));
      }
      callback();
    };

    let validatePass = (rule, value, callback) => {
      if(!value){
        return callback(new Error('密码不能为空'));
      }
      callback();
    };

    return {

        ruleForm2: {
          userName: '',
          password: ''
       },
       rules2: {
          userName: [
          { validator: validateUser, trigger: 'blur' }
          ],
          password: [
          { validator: validatePass, trigger: 'blur' }
          ]
        }

    };
  },
  methods: {
  	handle(){
      let vm = this;
      if(vm.ruleForm2.userName == ''|| vm.ruleForm2.password == ''){
        vm.$message.error('请输入用户名或密码');
        return ;
      }
      vm.$axios.get('/api/login',{
        params: {
          name: vm.ruleForm2.userName,
          password: vm.ruleForm2.password
        },
        method: 'get'
      }).then(res => {
          if(res.data.errcode == 0){
              vm.$message.success('登入成功！！！');
              vm.$router.replace('/project');
              return ;
          }
          return ;
      });
    }
  }

}
</script>

<style scoped>
.title{
	border-bottom:1px solid #E5E9F2;
}
.main{
	width: 500px;
	margin-left: auto;
	margin-right: auto;
	margin-top: 100px;
}
.button{
	width: 30%;
	margin-left: 35%;
	margin-right: 35%;
	margin-top: 18px;
}
</style>